package com.haixiaoke.saas.manage.lock.controller;

import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.saas.common.config.DevicesConfig;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.lock.domain.Model;
import com.haixiaoke.saas.lock.service.IModelService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 型号Controller
 *
 * @author xiaolong
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/manage/lock/model")
public class ManageModelController extends BaseController {
    @Autowired
    private IModelService modelService;
    @Autowired
    private DevicesConfig devicesConfig;

    /**
     * 查询型号列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Model model) {
        startPage();
        List<Model> list = modelService.selectModelList(model);
        return getDataTable(list);
    }


    /**
     * 查询型号列表
     */
    @GetMapping("/info")
    public AjaxResult info(Model model) {
        List<Model> list = modelService.selectModelList(model);
        return success(list);
    }

    /**
     * 获取型号详细信息
     */
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId) {
        return success(modelService.selectModelByModelId(modelId));
    }

    /**
     * 新增型号
     */
    @PostMapping
    public AjaxResult add(@RequestBody Model model) {
        model.setCreateBy(getUsername());
        return toAjax(modelService.insertModel(model));
    }

    /**
     * 修改型号
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Model model) {
        model.setUpdateBy(getUsername());
        return toAjax(modelService.updateModel(model));
    }

    /**
     * 删除型号
     */
    @DeleteMapping("/{modelId}")
    public AjaxResult remove(@PathVariable Long modelId) {
        return toAjax(modelService.deleteModelByModelId(modelId));
    }

    /**
     * 级联获取锁平台中品牌和型号
     */
    @GetMapping("/cascader")
    public AjaxResult cascader() {
        JSONObject jsonObject = devicesConfig.sendRequest(RequestMethod.GET, null, "/base/brand/cascader");
        return success(jsonObject.get("data"));
    }
}
